package com.example.demo.pdf.controller;

import com.example.demo.common.Result;
import com.example.demo.pdf.dto.PdfGenerateRequest;
import com.example.demo.pdf.service.PdfGeneratorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName PdfController
 * @Description PDF生成控制器
 * @Author MingChang.Wei
 * @Date 2025/9/26
 **/
@Slf4j
@RestController
@RequestMapping("/pdf")
@Tag(name = "PDF管理", description = "PDF文件生成相关接口")
public class PdfController {

    @Resource
    private PdfGeneratorService pdfGeneratorService;

    @PostMapping("/generate/template")
    @Operation(summary = "基于模板生成PDF", description = "使用指定模板和数据生成PDF文件")
    public Result<String> generateFromTemplate(@RequestBody @Valid PdfGenerateRequest request) {
        try {
            String outputPath = generateOutputPath(request.getFileName());
            pdfGeneratorService.generatePdfFromTemplate(
                    request.getTemplateName(),
                    request.getData(),
                    outputPath
            );
            return Result.success("PDF生成成功", outputPath);
        } catch (Exception e) {
            log.error("PDF生成失败", e);
            return Result.error("PDF生成失败: " + e.getMessage());
        }
    }

    @PostMapping("/generate/html")
    @Operation(summary = "基于HTML生成PDF", description = "将HTML内容转换为PDF文件")
    public Result<String> generateFromHtml(@RequestParam String htmlContent, 
                                         @RequestParam String fileName) {
        try {
            String outputPath = generateOutputPath(fileName);
            pdfGeneratorService.generatePdfFromHtml(htmlContent, outputPath);
            return Result.success("PDF生成成功", outputPath);
        } catch (Exception e) {
            log.error("HTML转PDF失败", e);
            return Result.error("HTML转PDF失败: " + e.getMessage());
        }
    }

    @PostMapping("/generate/text")
    @Operation(summary = "基于文本生成PDF", description = "将文本内容转换为PDF文件")
    public Result<String> generateFromText(@RequestParam String textContent, 
                                         @RequestParam String fileName) {
        try {
            String outputPath = generateOutputPath(fileName);
            pdfGeneratorService.generatePdfFromText(textContent, outputPath);
            return Result.success("PDF生成成功", outputPath);
        } catch (Exception e) {
            log.error("文本转PDF失败", e);
            return Result.error("文本转PDF失败: " + e.getMessage());
        }
    }

    @PostMapping("/generate/report")
    @Operation(summary = "生成报表PDF", description = "基于报表模板生成PDF文件")
    public Result<String> generateReport(@RequestBody @Valid PdfGenerateRequest request) {
        try {
            String outputPath = generateOutputPath(request.getFileName());
            pdfGeneratorService.generateReportPdf(
                    request.getData(),
                    request.getTemplateName(),
                    outputPath
            );
            return Result.success("报表PDF生成成功", outputPath);
        } catch (Exception e) {
            log.error("报表PDF生成失败", e);
            return Result.error("报表PDF生成失败: " + e.getMessage());
        }
    }

    @GetMapping("/demo")
    @Operation(summary = "PDF生成演示", description = "演示PDF生成功能")
    public Result<String> demo() {
        try {
            // 创建演示数据
            Map<String, Object> demoData = new HashMap<>();
            demoData.put("title", "系统演示报表");
            demoData.put("date", "2025-09-26");
            demoData.put("content", "这是一个PDF生成演示");
            
            String outputPath = generateOutputPath("demo_report");
            pdfGeneratorService.generatePdfFromTemplate("template.fo", demoData, outputPath);
            
            return Result.success("演示PDF生成成功", outputPath);
        } catch (Exception e) {
            log.error("演示PDF生成失败", e);
            return Result.error("演示PDF生成失败: " + e.getMessage());
        }
    }

    /**
     * 生成输出文件路径
     */
    private String generateOutputPath(String fileName) {
        // 确保文件名不包含扩展名
        if (fileName.endsWith(".pdf")) {
            fileName = fileName.substring(0, fileName.length() - 4);
        }
        
        // 添加时间戳避免文件名冲突
        String timestamp = String.valueOf(System.currentTimeMillis());
        return "G:" + File.separator + "pdf_output" + File.separator + fileName + "_" + timestamp + ".pdf";
    }
}
